package com.study.kt._01grammar._02oop

fun main() {
    dotoForWeek(Week.Thursday)

    val phone = TelPhone.Huawei
    println("${phone.pName}..${phone.price}")

    var goods = GoodsBean("1", "iphone")
    println(goods)

    //使用密封类
    isNormalVegetables(Tomato())
    isNormalVegetables(Vegetables.Broccoli())
}

//定义枚举
enum class Week {
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

//带构造的枚举
enum class TelPhone(var pName: String, var price: Int) {
    Xiaomi("小米", 1000) {
        override fun getPhone(): TelPhone = Xiaomi
    },
    IPhone("水果", 2000) {
        override fun getPhone(): TelPhone = IPhone
    },
    Huawei("华为", 2300) {
        override fun getPhone(): TelPhone = Huawei
    };

    abstract fun getPhone(): TelPhone
}

//使用枚举做判断
fun dotoForWeek(week: Week) {
    when (week) {
        Week.Monday, Week.Tuesday -> println("困")
        Week.Wednesday, Week.Thursday -> println("精力充沛")
        Week.Friday -> println("期待周末")
        Week.Saturday, Week.Sunday -> println("休息")
    }
}

//数据类就是用于保存数据的类，没有任何业务逻辑，类似JavaBean
//kotlin自动帮助我们生成getter、setter、hashCode、equal、copy、toString方法
//数据类型一般定义在一个kt文件中，方便管理
// 数据类自动生成equals/hashCode/toString方法
// 自动生成componentN方法，并且按照主构造方法中属性声明顺序生成的
data class GoodsBean(var id: String, var title: String)

// 如果要为数据类生成无参构造，就需要给属性提供默认值
data class UserBean(val id: String = "", var age: Int = 0)

/**
 * 定义密封类 密封类关注数据类型、枚举关注数据的值
 */
//定义蔬菜的密封类
sealed class Vegetables {
    //西蓝花是正经蔬菜
    class Broccoli : Vegetables()

    //茄子是正经蔬菜
    class Eggplant : Vegetables()
}

//西红柿可以当做水果也可以是蔬菜，所以不能在蔬菜的密封类中(密封类在kt1.1之后不需要定义在内部，只需要定义在同一文件即可)
class Tomato : Vegetables()

//判断蔬菜类型
fun isNormalVegetables(v: Vegetables) {
    when (v) {
        is Vegetables.Broccoli -> println("Broccoli")
        is Vegetables.Eggplant -> println("Vegetables")
        is Tomato -> println("Tomato")
        // when表达式一般需要else，而结合密封类则不需要使用else
        // else -> println("not vegetable")
    }
}